import 'package:sqflite/sqflite.dart';
import 'package:ymt/model/product.dart';

final String tableFavProduct = 'fav_products';
final String columnId = '_id';
final String columnProductId = 'id';
final String columnName = 'name';
final String columnThumbPicture= 'thumb_picture';
final String columnMainPicture= 'main_picture';
final String columnVisible = 'visible';
final String columnRetailRate = 'retail_rate';
final String columnCompanyId = 'company_id';
final String columnCategoryId = 'product_category_id';

class FavProduct {
  int id;
  String name;
  int retail_rate;
  int grandparent_id;
  String main_picture;
  String thumb_picture;
  int parent_id;
  int company_id;
  int product_category_id;
  int visible;

  Map<String, dynamic> toMap() {
    var map = <String, dynamic>{
      columnName: name,
      columnProductId: id,
      columnThumbPicture: thumb_picture,
      columnMainPicture: main_picture,
      columnVisible: visible,
      columnRetailRate: retail_rate,
      columnCompanyId: company_id,
      columnCategoryId: product_category_id,
    };
    return map;
  }

  FavProduct();

  FavProduct.fromMap(Map<String, dynamic> map) {
    id = map[columnProductId];
    name = map[columnName];
    visible = map[columnVisible];
    thumb_picture = map[columnThumbPicture];
    main_picture = map[columnMainPicture];
    retail_rate = map[columnRetailRate];
    company_id = map[columnCompanyId];
    product_category_id = map[columnCategoryId];
  }
}

class UserDatabaseProvider {
  Database db;

  Future open(String path) async {
    db = await openDatabase(path, version: 1,
        onCreate: (Database db, int version) async {
          await db.execute('''
create table $tableFavProduct ( 
  $columnId integer primary key autoincrement, 
  $columnProductId integer , 
  $columnName text,
  $columnThumbPicture text,
  $columnMainPicture text,
  $columnVisible integer,
  $columnRetailRate integer,
  $columnCompanyId integer,
  $columnCategoryId integer )
''');
        });
  }

  Future<int> insert(FavProduct product) async {
    product.id = await db.insert(tableFavProduct, product.toMap());
    return product.id;
  }

  Future<FavProduct> getTodo(int id) async {
    List<Map> maps = await db.query(tableFavProduct,
        columns: [columnId, columnVisible, columnName],
        where: '$columnId = ?',
        whereArgs: [id]);
    if (maps.length > 0) {
      return FavProduct.fromMap(maps.first);
    }
    return null;
  }

  Future<List<FavProduct>> all() async {
    List<Map> maps = await db.query(tableFavProduct);
    List<FavProduct> result = List();
    for (Map map in maps) {
      result.add(FavProduct.fromMap(map));
    }
    return result;
  }

  Future<List<Product>> allProduct() async {
    List<Map> maps = await db.query(tableFavProduct);
    List<Product> result = List();
    for (Map map in maps) {
      result.add(Product.fromMap(map));
    }
    return result;
  }

  Future<int> delete(int productId) async {
    return await db.delete(tableFavProduct, where: '$columnProductId = ?', whereArgs: [productId]);
  }

  Future<int> deleteAll() async {
    return await db.delete(tableFavProduct);
  }

  Future<int> update(FavProduct todo) async {
    return await db.update(tableFavProduct, todo.toMap(),
        where: '$columnId = ?', whereArgs: [todo.id]);
  }

  Future close() async => db.close();
}